//
//  SettingViewController.m
//  Remind
//
//  Created by zhufeng on 2017/10/25.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "SettingViewController.h"
#import "LoginViewController.h"
#import "EditSortViewController.h"
#import "LGSideMenuController.h"
#import "UIViewController+LGSideMenuController.h"
#import "DataCach.h"
#import "AppDelegate.h"

@interface SettingViewController () <UITableViewDelegate,UITableViewDataSource>
{
    BOOL setIfOpen;
    BOOL userIfOpen;
    BOOL sendNotific;
    NSArray *settingTextArr;
}
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) AppDelegate *appdelegate;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"菜单"] style:UIBarButtonItemStylePlain target:self action:@selector(showLeftMenu)];
     self.view.backgroundColor = [UIColor whiteColor];
    
    _appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    sendNotific = _appdelegate.sendNotifiction;
    
    //-----
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1.0];
    [self.view addSubview:_tableView];
    settingTextArr = @[@"设置",@"编辑分类",@"消息推送"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rowNum;
    rowNum = section==0&&userIfOpen?3:1;
    return setIfOpen&&section==1?3:rowNum;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
       
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(20, 49, CGRectGetWidth(tableView.frame)-20, 1)];
        line.backgroundColor = [UIColor lightGrayColor];
        [cell addSubview:line];
    }
    
    if(indexPath.section == 0){
        cell.textLabel.text = @"当前用户";
        cell.textLabel.textColor =  [UIColor colorWithRed:44/255.0 green:124/255.0 blue:246/255.0 alpha:1.0];
        if(indexPath.row == 1){
            
#pragma mark - 获取用户ID
            NSString *userID = [[DataCach getUserInformation] objectForKey:@"name"];
            userID = [@"ID:" stringByAppendingString:userID];
            NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:userID];
            NSRange range = [[attStr string] rangeOfString:@"ID:"];
            [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
            range = NSMakeRange(3, userID.length - 3);
            [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
            
            cell.textLabel.attributedText = attStr;
            return cell;
            
        }
        
        if(indexPath.row == 2){
            UITableViewCell *labelCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            labelCell.selectionStyle = UITableViewCellSelectionStyleNone;
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18, 18, CGRectGetWidth(_tableView.frame) - 36, 70 - 36)];
            label.backgroundColor = [UIColor redColor];
            label.layer.cornerRadius = 10.0;
            label.layer.masksToBounds = YES;
            label.textAlignment = NSTextAlignmentCenter;
            label.text = @"切换用户";
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
            [labelCell addSubview:label];
            return labelCell;
        }
        
        return cell;
    }
    
    cell.textLabel.text = settingTextArr[indexPath.row];
    cell.textLabel.textColor = [UIColor lightGrayColor];
    if(indexPath.row == 0){
        cell.textLabel.textColor = [UIColor colorWithRed:44/255.0 green:124/255.0 blue:246/255.0 alpha:1.0];
        return cell;
    }
    if(indexPath.row == 2){
        UITableViewCell *switchCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        switchCell.selectionStyle = UITableViewCellSelectionStyleNone;
        switchCell.textLabel.text = cell.textLabel.text;
        if(sendNotific) switchCell.textLabel.textColor = [UIColor colorWithRed:44/255.0 green:124/255.0 blue:246/255.0 alpha:1.0];
        else switchCell.textLabel.textColor = [UIColor lightGrayColor];

        UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(CGRectGetWidth(_tableView.frame) - 60, 8, 60, 50 - 16)];
        [switchBtn setOn:sendNotific animated:NO];;
        [switchBtn addTarget:self action:@selector(onClickSwitchBtn:) forControlEvents:UIControlEventValueChanged];
        
        [switchCell addSubview:switchBtn];
        return switchCell;
    }
    return cell;
}

#pragma mark - Switch点击事件
- (void)onClickSwitchBtn:(UISwitch *)sender{
    
    NSNumber *boolNumber = [NSNumber numberWithBool:sender.on];
    NSString *bol = boolNumber.boolValue?@"1":@"0";
    [DataCach setObject:bol forClassName:@"sendNotific" IfTheDataIsNullInitialized:[NSString class] ReplaceData:YES];
    _appdelegate.sendNotifiction = [bol boolValue];
    sendNotific = _appdelegate.sendNotifiction;
    [_tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            userIfOpen = !userIfOpen;
            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            return ;
        }
        if(indexPath.row == 2){
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            LoginViewController *loginCon = [story instantiateViewControllerWithIdentifier:@"loginCon"];
            [self.navigationController pushViewController:loginCon animated:YES];
            return ;
        }
        
    }else {
        
        //section == 1
        if(indexPath.row == 0){
            setIfOpen = !setIfOpen;
            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        }
        if(indexPath.row == 1){
            EditSortViewController *editCon = [[EditSortViewController alloc] init];
            [self.navigationController pushViewController:editCon animated:YES];
        }
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0&&indexPath.row==2){
        return 70;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    backGroundView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
    
    if(section == 0){
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 70, 20)];
        label.text = @"常规";
        label.textColor = [UIColor whiteColor];
        [backGroundView addSubview:label];
    }
    return backGroundView;
}

- (void)showLeftMenu{
    LGSideMenuController *sideMenuCon = [self sideMenuController];
    [sideMenuCon showLeftViewAnimated:YES completionHandler:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
